namespace Microsoft.Robotics.Tracing
{
    using System;
    using System.Collections.Generic;    

    /// <summary>
    /// Plain implementation of a trace dispatcher class
    /// todo: Dynamic enablement of tracing per category (based on context hierarchy) per level
    /// todo: use CorrelationManager.ActivityId to keep track of tracing/error reporting with multiple threads    
    /// </summary>
    public class SimpleTraceDispatcher : ITraceDispatcher
    {
        /// <summary>
        /// Currently registered tracers
        /// </summary>
        private List<ITraceWriter> tracers;

        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleTraceDispatcher" /> class with externally initialized tracers
        /// </summary>
        /// <param name="tracers">Trace writer to use</param>
        public SimpleTraceDispatcher(params ITraceWriter[] tracers)
        {
            this.tracers = new List<ITraceWriter>();

            foreach (ITraceWriter tracer in tracers)
            {
                this.tracers.Add(tracer);
            }
        }

        /// <summary>
        /// Dispatch to all tracers. This is where enable/disable logic will come (i.e. via some sort of filter object that would
        /// provide desired level of filtering
        /// </summary>
        /// <param name="level">Tracing level, i.e. Info, Warning, Error</param>
        /// <param name="context">Tracing context, i.e. logical component name</param>
        /// <param name="format">Format of the trace</param>
        /// <param name="args">The Arguments</param>
        public void Dispatch(TraceContext level, TraceContext context, string format, params object[] args)
        {
            if (null != this.tracers)
            {
                DateTime date = DateTime.Now;

                foreach (ITraceWriter tracer in this.tracers)
                {
                    tracer.WriteLine(date, level, context, format, args);
                }
            }
        }
    }
}
